package cn.btks.backstagemanage.log.service.impl;

import cn.btks.backstagemanage.log.pojo.SystemLog;
import cn.btks.backstagemanage.log.repository.SystemLogRepository;
import cn.btks.backstagemanage.log.service.SystemLogService;
import cn.btks.commonserver.pojo.PageList;
import cn.btks.commonserver.result.ResultState;
import cn.btks.commonserver.result.StateInfo;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author lijun
 * @Date 2021/1/4 18:18
 */
@Service
public class SystemLogServiceImpl implements SystemLogService {

    @Autowired
    SystemLogRepository systemLogRepository;

    @Override
    public ResultState findPagesByUserNameAndOperationName(Integer page, Integer pageSize, String userName, String operationName) {
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize);
        Specification<SystemLog> specification = new Specification<SystemLog>() {
            @Override
            public Predicate toPredicate(Root<SystemLog> root,
                                         CriteriaQuery<?> criteriaQuery,
                                         CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();
                if (!StringUtils.isEmpty(userName)) {
                    predicates.add(criteriaBuilder.like(root.get("userName"),
                            "%" + userName + "%"));
                }
                if (!StringUtils.isEmpty(operationName)) {
                    predicates.add(criteriaBuilder.equal(root.get("operationName"),operationName));
                }
                criteriaQuery.orderBy(criteriaBuilder.desc(root.get("creatDateTime")));
                Predicate[] predicates1 = new Predicate[predicates.size()];
                return criteriaBuilder.and(predicates.toArray(predicates1));
            }
        };
        Page<SystemLog> configPage = systemLogRepository.findAll(specification, pageRequest);
        if(configPage.getTotalElements() > 0){
            PageList pageList = new PageList();
            List<SystemLog> content = configPage.getContent();
            pageList.setResult(content);
            pageList.setPageNum(configPage.getTotalPages());
            pageList.setTotalCount(configPage.getTotalElements());
            return new ResultState(StateInfo.FIND_OK.getCode(), StateInfo.FIND_OK.getMsg(), pageList);
        }
        return new ResultState(StateInfo.FIND_NULL.getCode(), StateInfo.FIND_NULL.getMsg());
    }

    @Override
    public SystemLog save(SystemLog systemLog) {
        if(systemLog != null){
            SystemLog systemLog1 = systemLogRepository.save(systemLog);
            return systemLog1;
        }
        return null;
    }
}
